package cn.ffcs.unm.insurance.action;

import java.text.SimpleDateFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.ListModelList;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.unm.insurance.action.bean.InsurancePublicProjectMainBean;
import cn.ffcs.unm.insurance.manager.InsuranceTypeManager;
import cn.ffcs.unm.insurance.model.InsurancePublicProject;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;

@Controller
@Scope("prototype")
public class InsurancePublicProjectMainComposer extends BasePortletComposer {

	private static final long serialVersionUID = 664736049871429197L;

	private InsurancePublicProjectMainBean bean = new InsurancePublicProjectMainBean();

	private InsurancePublicProject qryInsurancePublicProject;
	
	@SuppressWarnings("unused")
	private InsurancePublicProject insurancePublicProject;

	@Autowired
	@Qualifier("insuranceTypeManager") // 业务层注入注解
	private InsuranceTypeManager insuranceTypeManager;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}
	/**
	 * 初始化
	 * @throws Exception
	 */
	public void onCreate$insurancePublicProjectMainWin() throws Exception {
		//this.onQuery();
	}

	public void onQuery() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		if (!StrUtil.isNullOrEmpty(bean.getMonthId().getValue())) {
			String monthId = formatter.format(bean.getMonthId().getValue());
			clearVariable();
			InsurancePublicProject objInsuranceProject = InsurancePublicProject.newInstance();
			PubUtil.fillPoFromBean(bean, objInsuranceProject);
			qryInsurancePublicProject = objInsuranceProject;
			int index = bean.getRadiogroup().getSelectedIndex();
			if (index == 0) {
				String monthIdMonth = monthId.substring(0, 6);
				qryInsurancePublicProject.setMonthId(monthIdMonth);
			}else {
				qryInsurancePublicProject.setMonthId(monthId);
			}
			this.bean.getInsurancePublicProjectListboxPaging().setActivePage(0);
			onInsurancePublicProjectQuery();
			insurancePublicProject = null;
		}else{
			ZkUtil.showError("请输入账期", "温馨提示");
		}
	}

	public void onReset() {
		bean.getName().setValue(null);
		bean.getCode().setValue(null);
		bean.getMonthId().setValue(null);
		clearVariable();
		ListboxUtils.clearListbox(this.bean.getInsurancePublicProjectListbox());
		this.bean.getInsurancePublicProjectListboxPaging().setTotalSize(1);
	}

	public void onInsurancePublicProjectQuery() {
		if (this.qryInsurancePublicProject != null) {
			int activePage = bean.getInsurancePublicProjectListboxPaging().getActivePage();
			int pageSize = bean.getInsurancePublicProjectListboxPaging().getPageSize();
			PageInfo pageInfo = insuranceTypeManager.queryPageInfoByInsurancePublicProject(qryInsurancePublicProject,
					activePage + 1, pageSize);
			ListModelList dataList = new BindingListModelList(pageInfo.getDataList(), true);
			bean.getInsurancePublicProjectListbox().setModel(dataList);
			bean.getInsurancePublicProjectListboxPaging().setTotalSize(NumericUtil.nullToZero(pageInfo.getTotalCount()));
		}
	}
	/**
	 * 清除变量
	 */
	public void clearVariable() {
		qryInsurancePublicProject = null;
		insurancePublicProject = null;
	}
	/**
	 * 翻页
	 */
	public void onListboxPaging() {
		this.onInsurancePublicProjectQuery();
	}

}
